

# to load data
library(rio)

# to process data
library(tidyverse)

# to make maps
library(ggmap)
library(maps)
library(mapdata)

# to combine maps
library(gridExtra)


# load data about types of fund
funds <- import("fund_types.csv")


### MAPS SHOWING CHANGE IN NUMBER OF NATIONAL MEASURES OVER TIME

# 1990
funds1990 <- funds %>%
  filter(year_begin<1991) %>%
  group_by(country) %>%
  summarise(N=length(country)) %>%
  rename(region = country) %>%
  ungroup()

# 2000
funds2000 <- funds %>%
  filter(year_begin<2001) %>%
  group_by(country) %>%
  summarise(N=length(country)) %>%
  rename(region = country) %>%
  ungroup()

# 2010
funds2010 <- funds %>%
  filter(year_begin<2011) %>%
  group_by(country) %>%
  summarise(N=length(country)) %>%
  rename(region = country) %>%
  ungroup()

# 2020
funds2020 <- funds %>%
  filter(year_begin<2021) %>%
  group_by(country) %>%
  summarise(N=length(country)) %>%
  rename(region = country) %>%
  ungroup()


# make maps
world <- map_data("world")
world <- world %>%
  filter(region != "Antarctica")

# 1990
world1990 <- full_join(world,funds1990,by=c("region"))
world1990$policy <- ifelse(is.na(world1990$N),0,1)
rest.world1990 <- world1990 %>%
  filter(is.na(N))

plot1990 <- ggplot(world1990, aes(long, lat, group = group)) +
  geom_polygon(color = "gray92", size = 0.001) +
  geom_polygon(data = rest.world1990, fill = "gray92", size = 0.001) +
  theme_void() + labs(title="1990") + 
  theme(plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(size=16,hjust = 0.5))

# 2000
world2000 <- full_join(world,funds2000,by=c("region"))
world2000$policy <- ifelse(is.na(world2000$N),0,1)
rest.world2000 <- world2000 %>%
  filter(is.na(N))

plot2000 <- ggplot(world2000, aes(long, lat, group = group)) +
  geom_polygon(color = "gray92", size = 0.001) +
  geom_polygon(data = rest.world2000, fill = "gray92", size = 0.001) +
  theme_void() + labs(title="2000") + 
  theme(plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(size=16,hjust = 0.5))

# 2010
world2010 <- full_join(world,funds2010,by=c("region"))
world2010$policy <- ifelse(is.na(world2010$N),0,1)
rest.world2010 <- world2010 %>%
  filter(is.na(N))

plot2010 <- ggplot(world2010, aes(long, lat, group = group)) +
  geom_polygon(color = "gray92", size = 0.001) +
  geom_polygon(data = rest.world2010, fill = "gray92", size = 0.001) +
  theme_void() + labs(title="2010") + 
  theme(plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(size=16,hjust = 0.5))

# 2020
world2020 <- full_join(world,funds2020,by=c("region"))
world2020$policy <- ifelse(is.na(world2020$N),0,1)
rest.world2020 <- world2020 %>%
  filter(is.na(N))

plot2020 <- ggplot(world2020, aes(long, lat, group = group)) +
  geom_polygon(color = "gray92", size = 0.001) +
  geom_polygon(data = rest.world2020, fill = "gray92", size = 0.001) +
  theme_void() + labs(title="2020") + 
  theme(plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(size=16,hjust = 0.5))

fourplots <- grid.arrange(plot1990,plot2000,plot2010,plot2020, ncol=2)

#ggsave("figure1.pdf", height = 10, width = 15, fourplots)
